//
//  OrderViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/27.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "OrderViewController.h"
#import "YygHomeViewController.h"
#import "DingDanZhanShiFooterView.h"
#import "YuEPayView.h"
@interface OrderViewController ()<MimaViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phone;

@end

@implementation OrderViewController
{
    UIView * _view;
    YuEPayView *_homeView;
    
    DingDanZhanShiFooterView * _footView;
    NSString * _dingDanId;
    UIScrollView * _scrView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawNavWithTitle:@"提交订单" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    //分享
    UIButton * guiButton = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-40, 32, 20, 20)];
    [guiButton setImage:[UIImage imageNamed:@"分享"]  forState:UIControlStateNormal];
        [guiButton addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:guiButton];
    self.view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
   _scrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64)];
    _scrView.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
    [self.view addSubview:_scrView];
    
   UIView * view = [[[NSBundle mainBundle]loadNibNamed:@"Order" owner:self options:nil] lastObject];
     _phone.text = [self defaultLoadUserPhone];
    [_scrView addSubview:view];
    //计算名称高度
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_firstModel.name];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:3];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,_firstModel.name.length)];
    //获取计算高度
    CGSize contentSize = [_firstModel.name boundingRectWithSize:CGSizeMake(KScreenWidth-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
    _H .constant =contentSize.height;
    _viewH.constant += (_H.constant-20);
    _name.text = _firstModel.name;
    _jia.layer.borderWidth=1;
    _jia.layer.borderColor=[UIColor lightGrayColor].CGColor;
    _jian.layer.borderWidth=1;
    _jian.layer.borderColor=[UIColor lightGrayColor].CGColor;
    _num.layer.borderWidth=1;
    _num.layer.borderColor=[UIColor lightGrayColor].CGColor;
    view.frame = CGRectMake(0, 8, KScreenWidth, 190+_H.constant);
    float h;
     if([WXApi isWXAppInstalled]){
         h = 150*_kscH;
     }else{
         h = 100*_kscH;
     }
    _footView = [[DingDanZhanShiFooterView alloc]initWithFrame:CGRectMake(0, view.bottom+10, KScreenHeight, h)];
    [_scrView addSubview:_footView];
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(20, _footView.bottom+25, KScreenWidth-40, 40)];
    [button setTitle:@"立即支付" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithHue:0.08 saturation:1.00 brightness:1.00 alpha:1.00];
    [button addTarget:self action:@selector(zhiFou) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [_scrView addSubview:button];
    _scrView.contentSize = CGSizeMake(KScreenWidth, button.bottom);
}
-(void)tanKuang{
    _homeView = [[YuEPayView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    _homeView.delegate = self;
    [_homeView.inputTextField becomeFirstResponder];
    [self.view addSubview:_homeView];
   }
- (IBAction)jia:(id)sender {
    
    int num = [_num.text intValue];
    num++;
    _num.text =[NSString stringWithFormat:@"%d",num];
    _money.text =[NSString stringWithFormat:@"¥%.2f",(float)num*1];
}
- (IBAction)jian:(id)sender {
    int num = [_num.text intValue];
    if (num>1) {
        num--;
        _num.text =[NSString stringWithFormat:@"%d",num];
        _money.text =[NSString stringWithFormat:@"¥%.2f",(float)num*1];
    }
   }
#pragma mark 编辑电话号码
- (IBAction)call:(id)sender {
    _phone.enabled = YES;
    [_phone becomeFirstResponder];
}

#pragma mark 一元购下单接口
-(void)zhiFou{
      NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
     [dic setObject:[self defaultLoadUserName] forKey:@"userName"];//用户名
     [dic setObject:_firstModel._id forKey:@"goodsId"];//商品ID
     [dic setObject:_num.text forKey:@"goodsNum"];//数量
     [dic setObject:_num.text forKey:@"totalFee"];//价钱
    
    [NetWorking NEWjieKouQingQiu:YYG_ordrYYG ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                _dingDanId = dic[Response];
                  int zhiFu = _footView.zhifuFangShiBJ;
                      switch (zhiFu) {
                        case 0://支付宝支付
                            break;
                        case 1://余额支付
                          {
                              [self tanKuang];
                              [UIView animateWithDuration:0.5 animations:^{
                                  _homeView.alpha = 1.0;
                                  _homeView.hidden = NO;
                              }];
                          }
                              break;
                        default://微信或其他支付
                            break;
                    }
                }
        }
    }];
}

-(void)queMiMaEnd{
     [self requestData];
    _homeView.hidden = YES;
    _homeView.alpha = 0.0;
}
#pragma mark 一元购支付接口
-(void)requestData{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[self defaultLoadUserName] forKey:@"userName"];//用户名
    [dic setObject:_homeView.inputTextField.text forKey:@"password"];//密码
    [dic setObject:_dingDanId forKey:@"orderId"];
    [dic setObject:_firstModel._id forKey:@"goodsId"];
    [dic setObject:_num.text forKey:@"goodsNum"];
    [dic setObject:_money.text forKey:@"totalFee"];
    [NetWorking NEWjieKouQingQiu:paiYYG ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        _homeView.inputTextField.text = @"";
        if (!error) {
            if ([dic[Code] intValue] == 200){
                [[iToast makeText:@"  支付成功!  "] show];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }];
}
#pragma mark 分享
-(void)shareButton{

}
@end
